﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using vconf_shared;
//using VidGrabNoForm.VideoGrabber;

namespace vconf_client
{
    static class VConfClient
    {
		
		public static String srvAddress = "localhost";
		public static int port = 8084;
		//full address name, at which to connect to
		public static String connectionAddress;
		
		public static IVConf obj;
		
		public static User me;
		
		static public Dictionary<String, User> users;
		
		//Wideo graber nasz. To przesylamy innym.
		private static VidGrabNoForm.VideoGrabber videoOur;

        //wideo graber jakiegos usera. To wyswietlamy u siebie.
        private static VidGrabNoForm.VideoGrabber videoSent;
		
		//ustawia adres serwera
		private static void setServerAddress(String addr) {
			srvAddress = addr;	
		}

        private static void setPort(int p)
        {
			port = p;	
		}
		
		public static void connect() {
			connectionAddress = "tcp://" + srvAddress + ":" + port + "/VConf";
			
			TcpChannel chan = new TcpChannel();

            // Tell the remoting system to use the channel
            ChannelServices.RegisterChannel(chan, false);

            // Create a transparent proxy "obj" for the remote object
            obj = (IVConf)Activator.GetObject(
                typeof(IVConf), // Remote object type
                connectionAddress); // Remote object URL

            if (obj == null)
                System.Console.WriteLine("Could not locate " +
                       "HelloServer type in RemoteServer.exe asembly");
            else
            {
				Console.WriteLine("Successfully connected to server");	
			}
		}

        public static void createUser(String userName)
        {
            me = new User(userName);
        }

        public static bool Register()
        {
            return obj.Register(me);
        }

        public static bool Unregister()
        {
            return obj.Unregister(me);
        }

        public static bool AssignStreamURL(String url) 
        {
            return obj.AssignStreamURL(me, url);
        }

        public static void SendHeartBeat()
        {
            if (me != null)
            {
                obj.SendHeartbeat(me);
            }
        }
		
		//pobiera z serwera liste userow
		public static void getUsers() {
                users = obj.GetActiveUsers(me);
		}
		
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            //! TODO
            // GUI musi miec mozliwosc podawania nazwy usera! !
            //createUser("trololo");

            //VConfClient.connect();

            //VConfClient.getUsers();

            //VConfClient.startVideo();
            
           
        }
    }
}
